#!/usr/bin/env xpcshell

/**
 * Afrimesh: easy management for B.A.T.M.A.N. wireless mesh networks
 * Copyright (C) 2008-2009 Meraka Institute of the CSIR
 * All rights reserved.
 *  
 * This software is licensed as free software under the terms of the
 * New BSD License. See /LICENSE for more information.
 */


/** TODO rewrite so that this happens in the unit test, not the library code  */
var platform = undefined;
if (XMLHttpRequest === undefined) {
  load("unit/shell.js");
  var XMLHttpRequest = Shell.XMLHttpRequest;
  var setTimeout = Shell.setTimeout;
  var sleep      = Shell.sleep; 
} else {
  var sleep = undefined; // TODO
}

load("unit/utilities.js");

load("../dashboard/javascript/afrimesh.utilities.js");
load("../dashboard/javascript/afrimesh.villagebus.js");
load("../dashboard/javascript/afrimesh.network.js");
load("../dashboard/javascript/afrimesh.customers.js");
load("../dashboard/javascript/afrimesh.js");


/** - TODO - add toString to Route class ---------------------------------- */
function printRoute(route) {
  print("\t" + route.router + "\t->\t" + (route.neighbour ? route.neighbour : route.gateway) + "\t(" + route.label + ")");
};


/** - afrimesh.settings --------------------------------------------------- */
print("afrimesh.settings.locale = ", afrimesh.settings.locale);
for (host in afrimesh.settings.hosts) {
  print("afrimesh.settings.hosts." + host + " = " + afrimesh.settings.hosts[host]); 
}

  
/** - afrimesh.network ---------------------------------------------------- */
afrimesh.network.backbone.routers().map(function(router) { 
    print("afrimesh.network.backbone.router=" + router.address); 
    router.routes.map(printRoute);
  });
afrimesh.network.mesh.routers().map(function(router) { 
    print("afrimesh.network.mesh.router=" + router.address); 
    router.routes.map(printRoute);
  });
afrimesh.network.routers().map(function(router) { 
    print("afrimesh.network.router=" + router.address); 
    router.routes.map(printRoute);
  });
print("afrimesh.network.backbone= \\");
afrimesh.network.backbone().map(printRoute);
print("afrimesh.network.mesh= \\");
afrimesh.network.mesh().map(printRoute);
print("afrimesh.network= \\");
afrimesh.network().map(printRoute);


/** - afrimesh.villagebus ------------------------------------------------- */
print("afrimesh.villagebus = ", afrimesh.villagebus());

// sync calls
print("afrimesh.villagebus.batman= \\");    afrimesh.villagebus.batman().map(printRoute);
print("afrimesh.villagebus.batman.vis=", afrimesh.villagebus.batman.vis());

// async calls
afrimesh.villagebus.batman.vis.async(function(routers) {
    print("afrimesh.villagebus.batman.vis.async=", routers);
  });

// polling calls
afrimesh.villagebus.batman.vis.poll(function(routers) {
    print("afrimesh.villagebus.batman.vis.poll=", routers);
  }, 1000);


/** - spend five seconds processing any events generated by this test --- */
setTimeout(function() { Shell.exit(); }, 5000);
Shell.main();
print("fin.");  


